package net.hn.hnms.biz.safety.trinity.dao;

import net.hn.hnms.biz.safety.trinity.entity.WorkSafetyViolation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author 智呈物联 nzw
 * @date 2022/8/5
 */
@Repository
public interface WorkSafetyViolationDao extends JpaRepository<WorkSafetyViolation,String> {

    @Query(value = "SELECT\n" +
            "\tviolation_level,\n" +
            "\tCOUNT ( violation_level )\n" +
            "from\n" +
            "\twork_safety_violation \n" +
            "GROUP BY\n" +
            "\tviolation_level",nativeQuery = true)
    List<Map<String,Object>> threeViolations();

    @Query(value = "SELECT COUNT\n" +
            "\t( * ) \n" +
            "FROM\n" +
            "\twork_safety_violation",nativeQuery = true)
    int countAll();

    @Query(value = "SELECT COUNT\n" +
            "\t( * ) \n" +
            "FROM\n" +
            "\twork_safety_violation "+
            " where CASE when ?1 != '' then company_code = ?1 else 1=1 end",nativeQuery = true)
    int countAllByCompanyCode(String companyCode);



    @Query(value = "SELECT\n" +
            "\tviolation_level,\n" +
            "\tCOUNT ( violation_level )\n" +
            "from\n" +
            "\twork_safety_violation \n" +
            " where CASE when ?1 != '' then company_code = ?1 else 1=1 end "+
            "GROUP BY\n" +
            "\tviolation_level ",nativeQuery = true)
    List<Map<String,Object>> threeViolationsByCompanyCode(String companyCode);

}
